const app = getApp();
Page({
  data: {
    view:'submit',
    followMan: "",
    id:"",
    db:"",
    rid:"",
    followData:"",
    mid:"",
    departmentid:"",
    createMan : "",
    createUid : "",
    createDid : "",
  },

onLoad(query) {
    var that =this;
    var url = app.globalData.url;
    dd.httpRequest({ 
            url:url+'/api/hr/weekly/followMan',
            method:'post',
            datatype:'json',
            data:{
              followMan:query.followMan,
            },
            success:function(res){
              res.data.data.forEach(function(index){
                  that.setData({
                    mid:index.userId,
                    departmentid:index.deptId,
                    followMan:query.followMan,
                    createMan : query.createMan,
                    createUid : query.createUid,
                    createDid : query.createDid,
                  });
              })
            } 
        });
    if(query.id!=undefined){
        that.setData({
          flag:'modify',
          id:query.id,
          rid:query.rid,
          goal: query.goal,
          result:query.result,
          plan:query.plan,
          followData:query.followData,
          // followManIndex:fi,
      })
    }else{
        dd.httpRequest({
          url: url+'/api/hr/weekly/yearMonth',
          method: 'POST',
          data: {
          },
          dataType: 'json',
          success: function(res) {
                that.setData({
                  flag:'submit',
                  rid:query.rid,
                  followData:res.data,
                  db:query.db,
              })
          },
          fail: function(res) {
            dd.alert({content: 'fail'});
          },
          complete: function(res) {
            // dd.alert({content: 'complete'});
          }
        });
    }
},

  
  onShow(options) {
   
  },

  submit(e) {
    var goal =e.detail.value.goal;
    var result =e.detail.value.result;
    var plan =e.detail.value.plan;
    var url = app.globalData.url;
    var that =this;
    if(goal==''){
       dd.showToast({
          content: '请完善客户拜访目的！', 
        });
        return;
    }
     if(result==''){
       dd.showToast({
          content: '请完善达成意向结果！', 
        });
        return;
    }
     if(plan==''){
       dd.showToast({
          content: '请完善推进计划！',
        });
        return;
    }
    
    if(that.data.id == '' || that.data.id == null){
        url = url+'/api/hr/weekly/add';
    }else{
        url = url+'/api/hr/weekly/edit?id='+that.data.id;
    }
    dd.httpRequest({
      url: url,
      method: 'POST',
      dataType: 'json',
      data: {
        goal:goal,
        result:result,
        plan:plan,
        followData:that.data.followData,
        rid:that.data.rid,
        followMan:that.data.followMan,
        mid:that.data.mid,
        departmentid:that.data.departmentid,
        createMan : that.data.createMan,
        createUid : that.data.createUid,
        createDid : that.data.createDid,
      },
      success: function(res) {
        var thats =that;
        dd.httpRequest({ 
            url:app.globalData.url+'/api/hr/weekly/editReport',
            method:'post',
            datatype:'json',
            data:{
              rId: thats.data.rid,
              followData:thats.data.followData,
              state:3,
            },
            success:function(res){
              dd.alert({content: '周报已提交'});
                      that.setData({
                        flag:'ban',
                    })
            }
         })   
        
        if(that.data.db==""){
            dd.navigateTo({
            url: '../summary/summary',
          })
        }else{
          dd.navigateTo({
            url: '../../../daiban/weekdaiban/weekdaiban',
          })
        }

      },
      fail: function(res) {
        dd.alert({content: 'fail'});
      },
      complete: function(res) {
        // dd.alert({content: 'complete'});
      }
    });
  },
 
  onReset() {},

  
});
